//
//  IconViewController.m
//  FNer
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "IconViewController.h"
#import "JXLMenuView.h"
#import "RESideMenu.h"
#import "IconCollectionViewCell.h"
#import "IconDetailViewController.h"
@interface IconViewController ()<JXLMenuViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,iconDetailDelegate>

@property (nonatomic, strong) NSMutableArray *titles;
@property(nonatomic,strong)JXLMenuView * jxlMenuView;

@property(nonatomic,retain)BaseCollectionView *iconCV;
@property(nonatomic,retain)NSMutableArray *userArray;
@property(nonatomic,copy)NSString *page;

@property(nonatomic,retain)UILabel *titleLabel;


@end

@implementation IconViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
   
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    self.titleLabel.text = @"全部";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18 * XWIDTH];
    self.navigationItem.titleView = self.titleLabel;
    self.navigationController.navigationBar.translucent = NO;
    
    //左方返回按钮
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    self.titles = [NSMutableArray arrayWithObjects:@"全部",@"摄影师",@"旅行家",@"女神范",@"文艺派",@"时尚咖",@"乐活族",@"插画师",@"美食家", nil];
    
    //创建达人collectionView
    [self createIconCV];
    //自动下拉刷新
    [self addHeaderRefresh];

    
    // 创建JXLMenuView
    self.jxlMenuView = [[JXLMenuView alloc] initWithFrame:CGRectMake(-100, 0, self.view.frame.size.width+100, self.view.frame.size.height-64)];
    self.jxlMenuView.titleColor = [UIColor grayColor]; // 未选中时的标题文字颜色，默认grayColor
    self.jxlMenuView.titleSelectedColor = [UIColor blackColor]; // 选中时的标题文字颜色，默认blackColor
    self.jxlMenuView.menuWith = 100.f; // menu栏的宽度，默认宽度100
    self.jxlMenuView.delegate = self;
    self.jxlMenuView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.jxlMenuView];
    

    

    
    
    
}
-(void)addHeaderRefresh
{
    
     
    self.iconCV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  
        [self getIconData];
    }];
    
    [self.iconCV.header beginRefreshing];
}
-(void)pushIconDetail:(User *)auser indexPath:(NSInteger)page
{
    IconDetailViewController *iconDetailVC = [[IconDetailViewController alloc] init];
    [self.navigationController pushViewController:iconDetailVC animated:YES];
    iconDetailVC.auser = auser;
    iconDetailVC.page = page;
}

//获取达人数据
-(void)getIconData
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/operations/user_queryVerifyRecommendUser?&maxId=0&start=1&limit=10&verifyId=%@&worldLimit=3",self.page];
    [AFNTools getAFN:url_string andpara:nil andBlock:^(id result) {
        self.userArray = [NSMutableArray array];
        [self.userArray removeAllObjects];
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"userInfo"];
        for (NSMutableDictionary *dic in array) {
            User *auser = [[User alloc] init];
            [auser setValuesForKeysWithDictionary:dic];
            [self.userArray addObject:auser];
            
        }
        
        [self.iconCV.header endRefreshing];
        [self.iconCV reloadData];
    } andfailBlock:^(id fail) {
        
        NSLog(@"没有网络");
        [self.iconCV.header endRefreshing];
        
        
    }];

}




-(void)createIconCV
{
 
    self.page = @"0";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH - 20 * XWIDTH, 200 * XHIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * XHIGHT, 10 * XWIDTH, 10 * XHIGHT, 10 * XWIDTH);
    
    self.iconCV = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT - 64) collectionViewLayout:flowLayout];
    self.iconCV.delegate = self;
    self.iconCV.dataSource = self;
    [self.iconCV registerClass:[IconCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
   [self.iconCV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"];
    
    
    //轻扫手势
    UISwipeGestureRecognizer * Rightswip=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
    [self.iconCV  addGestureRecognizer :Rightswip];
    
    UISwipeGestureRecognizer * Leftswip=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
    Leftswip.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.iconCV  addGestureRecognizer :Leftswip];


   
    
}

-(void)swipAction:(UISwipeGestureRecognizer *)swip
{
    
    if (swip.direction==UISwipeGestureRecognizerDirectionRight) {
        
        [self leftShow];
    }else if (swip.direction==UISwipeGestureRecognizerDirectionLeft)
    {
        [self leftHide];
    }
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.userArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IconCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.auser = [self.userArray objectAtIndex:indexPath.row];
    
    cell.myAgent = self;
    
    return cell;
}








//点击左方返回按钮触发
-(void)leftButtonAction
{
    [self.sideMenuViewController presentLeftMenuViewController];
}
-(void)leftHide
{
    
    self.navigationItem.rightBarButtonItem.title=@"开启";
    [UIView animateWithDuration:0.5 animations:^{
        CGRect temRect=self.jxlMenuView.frame;
        temRect.origin.x =-100;
        self.jxlMenuView.frame=temRect;
        
    }];
}
-(void)leftShow
{
    
    self.navigationItem.rightBarButtonItem.title=@"隐藏";
    [UIView animateWithDuration:0.5 animations:^{
        CGRect temRect=self.jxlMenuView.frame;
        temRect.origin.x =0;
        self.jxlMenuView.frame=temRect;
    }];
    

}
#pragma mark -
#pragma mark JXLMenuDelegate
#pragma mark @required

- (NSInteger)numberOfRowsInMenuView:(JXLMenuView *)menuView {
    return self.titles.count;
}

- (NSString *)menuView:(JXLMenuView *)menuView titleOfRow:(NSInteger)menuIndex {
    return self.titles[menuIndex];
}


- (UIView *)menuView:(JXLMenuView *)menuView centerViewForIndex:(NSInteger)menuIndex {
    
     return self.iconCV;
}


#pragma mark @optional

- (CGFloat)menuView:(JXLMenuView *)menuView heightForRow:(NSInteger)menuIndex {
    return 65.f;
}

- (void)menuView:(JXLMenuView *)menuView didSelectRowAtIndex:(NSInteger)menuIndex {
    // You can get centerView use menuView.centerView
      self.titleLabel.text = [self.titles objectAtIndex:menuIndex];
    [self leftHide];
  
    
    if (0 == menuIndex) {
        self.page = @"0";
        
    }
    else if (1 == menuIndex)
    {
        self.page = @"3";
    }
    else if (2 == menuIndex)
    {
        self.page = @"5";
    }
    else if (3 == menuIndex)
    {
        self.page = @"16";
    }
    else if (4 == menuIndex)
    {
        self.page = @"15";
    }
    else if (5 == menuIndex)
    {
        self.page = @"4";
    }
    else if (6 == menuIndex)
    {
        self.page = @"2";
    }
    else if (7 == menuIndex)
    {
        self.page = @"10020";
    }
    else if (8 == menuIndex)
    {
        self.page = @"10021";
    }
    
 [self addHeaderRefresh];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
